<template>
  <div class="header">
    <h1 class="module-title">文本对比</h1>
  </div>

  <div class="text-input-section">
    <div class="text-box">
      <div class="text-box-header">
        <span>文本1</span>
        <div class="text-box-actions">
          <button class="action-button" @click="clearText1">清空</button>
        </div>
      </div>
      <div class="text-area-wrapper">
        <div class="line-numbers" ref="lineNumbers1"></div>
        <div 
          class="text-area result-area" 
          v-html="text1Result"
        ></div>
        <textarea 
          ref="textarea1"
          v-model="text1" 
          class="text-area" 
          placeholder="在此输入或粘贴原始文本..."
          @input="updateLineNumbers1"
          @scroll="syncScroll1"
        ></textarea>
      </div>
    </div>

    <div class="text-box">
      <div class="text-box-header">
        <span>文本2</span>
        <div class="text-box-actions">
          <button class="action-button" @click="clearText2">清空</button>
        </div>
      </div>
      <div class="text-area-wrapper">
        <div class="line-numbers" ref="lineNumbers2"></div>
        <div 
          class="text-area result-area" 
          v-html="text2Result"
        ></div>
        <textarea 
          ref="textarea2"
          v-model="text2" 
          class="text-area" 
          placeholder="在此输入或粘贴修改后的文本..."
          @input="updateLineNumbers2"
          @scroll="syncScroll2"
        ></textarea>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { invoke } from '@tauri-apps/api/core';
import type { ApiResponse } from "../types";

const text1 = ref('');
const text2 = ref('');
const lineNumbers1 = ref<HTMLElement | null>(null);
const lineNumbers2 = ref<HTMLElement | null>(null);
const textarea1 = ref<HTMLTextAreaElement | null>(null);
const textarea2 = ref<HTMLTextAreaElement | null>(null);
const text1Result = ref('');
const text2Result = ref('');

// 计算文本差异
const computeDiff = async () => {
  try {
    const response = await invoke<ApiResponse>("compute_text_diff", {
      text1: text1.value,
      text2: text2.value
    });
    
    if (response.success) {
      const result = JSON.parse(response.result);

      console.log('result', result);
      text1Result.value = result.text1Result;
      text2Result.value = result.text2Result;
    }
  } catch (error) {
    console.error('计算差异失败:', error);
  }
};

// 监听文本变化
watch([text1, text2], () => {
  computeDiff();
});

// 更新行号
const updateLineNumbers = (text: string, lineNumbersElement: HTMLElement) => {
  const lines = text.split('\n');
  const lineCount = Math.max(lines.length, 1);
  lineNumbersElement.innerHTML = Array.from({ length: lineCount }, (_, i) => 
    `<div class="line-number">${i + 1}</div>`
  ).join('');
};

const updateLineNumbers1 = () => {
  if (lineNumbers1.value) {
    updateLineNumbers(text1.value, lineNumbers1.value);
  }
};

const updateLineNumbers2 = () => {
  if (lineNumbers2.value) {
    updateLineNumbers(text2.value, lineNumbers2.value);
  }
};

// 同步滚动
const syncScroll = (textarea: HTMLTextAreaElement, lineNumbersElement: HTMLElement, resultArea: HTMLElement) => {
  lineNumbersElement.scrollTop = textarea.scrollTop;
  resultArea.scrollTop = textarea.scrollTop;
  resultArea.scrollLeft = textarea.scrollLeft;
};

const syncScroll1 = (event: Event) => {
  const textarea = event.target as HTMLTextAreaElement;
  if (lineNumbers1.value) {
    const resultArea = textarea.previousElementSibling as HTMLElement;
    syncScroll(textarea, lineNumbers1.value, resultArea);
  }
};

const syncScroll2 = (event: Event) => {
  const textarea = event.target as HTMLTextAreaElement;
  if (lineNumbers2.value) {
    const resultArea = textarea.previousElementSibling as HTMLElement;
    syncScroll(textarea, lineNumbers2.value, resultArea);
  }
};

// 清空文本
const clearText1 = () => {
  text1.value = '';
  updateLineNumbers1();
};

const clearText2 = () => {
  text2.value = '';
  updateLineNumbers2();
};

// 组件挂载时初始化
onMounted(() => {
  updateLineNumbers1();
  updateLineNumbers2();
});
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.module-title {
  font-size: 18px;
  font-weight: 500;
  color: #2c3e50;
  margin-bottom: 12px;
}

.text-input-section {
  display: flex;
  gap: 20px;
  flex: 1;
  min-height: 0;
}

.text-box {
  flex: 1;
  display: flex;
  flex-direction: column;
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow: hidden;
  background-color: white;
  max-height: 600px;
}

.text-box-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ddd;
}

.text-box-actions {
  display: flex;
  gap: 8px;
}

.action-button {
  padding: 4px 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: white;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.action-button:hover {
  background-color: #f0f0f0;
}

.text-area-wrapper {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
  background-color: white;
}

.line-numbers {
  width: 40px;
  background-color: #f5f5f5;
  border-right: 1px solid #ddd;
  padding: 8px 8px;
  font-size: 14px;
  color: #666;
  overflow-y: hidden;
  user-select: none;
  flex-shrink: 0;
  line-height: 1.5;
  font-family: 'Fira Code', monospace;
  text-align: right;
}

.line-number {
  /*height: 21px; /* 14px * 1.5 = 21px */
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 8px;
  font-size: 14px;
  line-height: 1.5;
  font-family: 'Fira Code', monospace;
  text-align: right;
}

.text-area {
  flex: 1;
  margin: 0;
  padding: 8px;
  border: none;
  resize: none;
  font-family: 'Fira Code', monospace;
  font-size: 14px;
  line-height: 1.5;
  outline: none;
  background-color: transparent;
  color: transparent;
  caret-color: #333;
  position: absolute;
  top: 0;
  left: 40px;
  right: 0;
  bottom: 0;
  z-index: 2;
  tab-size: 4;
  -moz-tab-size: 4;
  box-sizing: border-box;
  white-space: pre;
  overflow-wrap: normal;
  word-wrap: normal;
  word-break: normal;
  overflow-x: auto;
  overflow-y: auto;
}

.text-area.word-wrap {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.result-area {
  position: absolute;
  top: 0;
  left: 40px;
  right: 0;
  bottom: 0;
  overflow-y: auto;
  white-space: pre;
  pointer-events: none;
  margin: 0;
  padding: 8px;
  font-family: 'Fira Code', monospace;
  font-size: 14px;
  line-height: 1.5;
  z-index: 1;
  tab-size: 4;
  -moz-tab-size: 4;
  box-sizing: border-box;
  overflow-wrap: normal;
  word-wrap: normal;
  word-break: normal;
  overflow-x: auto;
  color: #333;
}

:deep(.unchanged) {
  color: #333;
  margin: 0;
  padding: 0;
  display: inline;
}

:deep(.added) {
  background-color: #e6ffe6;
  color: #2ecc71;
  margin: 0;
  padding: 0;
  display: inline;
}

:deep(.removed) {
  background-color: #ffe6e6;
  color: #e74c3c;
  margin: 0;
  padding: 0;
  display: inline;
}

:deep(br) {
  margin: 0;
  padding: 0;
  line-height: 1.5;
  content: "";
  display: block;
}

:deep(span) {
  margin: 0;
  padding: 0;
  display: inline;
}

@media (prefers-color-scheme: dark) {
  .module-title {
    color: #ecf0f1;
  }

  .text-box {
    border-color: #34495e;
    background-color: #1a1a1a;
  }

  .text-box-header {
    background-color: #2c3e50;
    border-color: #34495e;
    color: #ecf0f1;
  }

  .action-button {
    background-color: #34495e;
    border-color: #2c3e50;
    color: #ecf0f1;
  }

  .action-button:hover {
    background-color: #2c3e50;
  }

  .line-numbers {
    background-color: #2c3e50;
    border-color: #34495e;
    color: #bdc3c7;
  }

  .text-area {
    background-color: #1a1a1a;
    color: #ecf0f1;
    caret-color: #ecf0f1;
  }

  .result-area {
    color: #ecf0f1;
  }

  :deep(.unchanged) {
    color: #ecf0f1;
  }

  :deep(.added) {
    background-color: rgba(46, 204, 113, 0.2);
  }

  :deep(.removed) {
    background-color: rgba(231, 76, 60, 0.2);
  }
}
</style> 